//
//  CollectionViewController.m
//  Emoticon
//
//  Created by ytkjs2 on 2020/4/1.
//  Copyright © 2020 ytk. All rights reserved.
//

#import "CollectionViewController.h"
#import "ListViewCell.h"
#import "LoverViewCell.h"
#import "SwitchView.h"
#import "SearchDownloadController.h"

@interface CollectionViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
{
    SwitchView * switchView;
}
@property (nonatomic, strong)NSArray * emoticonArray, * paperArray, * loverArray, * searchArray, * searchBagListArray;
@end

@implementation CollectionViewController


- (UIScrollView *)scroll
{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, rScreenWidth, rScreenHeight)];
        _scroll.delegate = self;
        _scroll.bounces = NO;
        _scroll.contentSize = CGSizeMake(rScreenWidth * 3, 0);
        _scroll.pagingEnabled = YES;
        
        [_emoticonCollectionView setFrame:CGRectMake(0, 0, rScreenWidth, rScreenHeight)];
        [_scroll addSubview:_emoticonCollectionView];
        
        [_paperCollectionView setFrame:CGRectMake(rScreenWidth, 0, rScreenWidth, rScreenHeight)];
        [_scroll addSubview:_paperCollectionView];
        
        [_loverTableView setFrame:CGRectMake(rScreenWidth * 2, 0, rScreenWidth, rScreenHeight)];
        [_scroll addSubview:_loverTableView];
        
        [_searchCollectionView setFrame:CGRectMake(rScreenWidth * 3, 0, rScreenWidth, rScreenHeight)];
        [_scroll addSubview:_searchCollectionView];
        
        [_searchBagListCollectionView setFrame:CGRectMake(rScreenWidth * 4, 0, rScreenWidth, rScreenHeight)];
        
        [_scroll addSubview:_searchBagListCollectionView];
    }
    return _scroll;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup afte r loading the view from its nib.
    
    _scroll.contentSize = CGSizeMake(rScreenWidth * 3, 0);
    
    switchView = [[SwitchView alloc]initWithFrame:CGRectMake((rScreenWidth - 250)/2, 0, 250, 55) withTitleArray:@[@"斗图",@"精选壁纸",@"情侣壁纸",@"搜索",@"表情包"]];
    switchView.controller = self;
    self.navigationItem.titleView = switchView;
    
    [self.emoticonCollectionView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    [self.paperCollectionView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    [self.searchCollectionView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    [self.searchBagListCollectionView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    [self.view addSubview:self.scroll];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    _emoticonArray = [NSArray arrayWithArray:[[PictureCache sharedDataBase]selectEmoticon]];
    _paperArray = [NSArray arrayWithArray:[[PictureCache sharedDataBase]selectAllPaper]];
    _loverArray = [NSArray arrayWithArray:[[PictureCache sharedDataBase]selectAllLover]];
    _searchArray = [NSArray arrayWithArray:[[PictureCache sharedDataBase]selectAllSearch]];
    _searchBagListArray = [NSArray arrayWithArray:[[PictureCache sharedDataBase]selectAllSearchBag]];
    
    [_emoticonCollectionView reloadData];
    [_paperCollectionView reloadData];
    [_loverTableView reloadData];
    [_searchCollectionView reloadData];
    [_searchBagListCollectionView reloadData];
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _emoticonCollectionView)
        return _emoticonArray.count;
    else if(collectionView == _paperCollectionView)
        return _paperArray.count;
    else if (collectionView == _searchCollectionView)
        return _searchArray.count;
    else
        return _searchBagListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ListViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListViewCell" forIndexPath:indexPath];
    
    NSString * imgUrl = @"";
    
    if (collectionView == _emoticonCollectionView) {
        PicModel * model = _emoticonArray[indexPath.item];
        imgUrl = model.image;
    }
    else if (collectionView == _paperCollectionView){
        PicModel * model = _paperArray[indexPath.item];
        imgUrl = model.image;
    }
    else if (collectionView == _searchCollectionView){
        SearchModel * model = _searchArray[indexPath.item];
        imgUrl = model.big_url;
    }
    else{
        SearchBagListModel * model = _searchBagListArray[indexPath.item];
        imgUrl = model.big_url;
    }
    
    imgUrl = [imgUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [cell.IMG sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"icon_placeholderImage"]];
    
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//上下行
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//行距
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(6.5, 12, 6.4, 12 );//分别为上、左、下、右
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.paperCollectionView) {
        CGFloat width = (rScreenWidth - 40)/3;
        
        return CGSizeMake(width,width * 2436/1370);
    }
    CGFloat width = (rScreenWidth - 61)/4;
    
    return CGSizeMake(width,width);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _emoticonCollectionView || collectionView == _paperCollectionView) {
        PicModel * model = [[PicModel alloc]init];
        DownloadViewController * downloadVC = [[DownloadViewController alloc]initWithNibName:@"DownloadViewController" bundle:nil];
        if (collectionView == _emoticonCollectionView) {
//            model = _emoticonArray[indexPath.item];
            downloadVC.dataSource = [_emoticonArray mutableCopy];
        }
        else
        {
//            model = _paperArray[indexPath.item];
            downloadVC.dataSource = [_paperArray mutableCopy];
            downloadVC.isLongPic = YES;
        }
        
        downloadVC.picIndex = indexPath.item;
        [self.navigationController pushViewController:downloadVC animated:YES];
    } else {
        SearchDownloadController * downloadVC = [[SearchDownloadController alloc]initWithNibName:@"SearchDownloadController" bundle:nil];
        
        if (collectionView == _searchCollectionView) {
            downloadVC.dataSource = [_searchArray mutableCopy];
        }
        else
        {
            downloadVC.dataSource = [_searchBagListArray mutableCopy];
            downloadVC.isBag = YES;
        }
        downloadVC.picIndex = indexPath.item;
        [self.navigationController pushViewController:downloadVC animated:YES];
    }
}

#pragma mark <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _loverArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (rScreenWidth - 27)/2;
    return width * rScreenHeight/rScreenWidth + 9;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rScreenWidth, 45)];
    view.backgroundColor = [UIColor clearColor];
    
    UIView * cornerView = [[UIView alloc]initWithFrame:CGRectMake(0, 5, rScreenWidth, 40)];
    cornerView.backgroundColor = [UIColor whiteColor];
    [self setViewCornerRadius:cornerView isHead:YES];
    [view addSubview:cornerView];
    
    LoverPicModel * model = _loverArray[section];
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 200, 20)];
    lab.text = model.title;
    lab.font = [UIFont boldSystemFontOfSize:14];
    [cornerView addSubview:lab];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rScreenWidth, 15)];
    view.backgroundColor = [UIColor clearColor];
    
    UIView * cornerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rScreenWidth, 10)];
    cornerView.backgroundColor = [UIColor whiteColor];
    [self setViewCornerRadius:cornerView isHead:NO];
    [view addSubview:cornerView];
    
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoverViewCell * cell = [[NSBundle mainBundle]loadNibNamed:@"LoverViewCell" owner:nil options:nil][0];
    cell.controller = self;
    LoverPicModel * model = _loverArray[indexPath.section];
    [cell setBtnWithImage:model.image withTag:indexPath.section];
    return cell;
}

-(void)btnAction:(UIButton *)btn
{
    NSLog(@"%ld",btn.tag);
    NSInteger section = btn.tag/2;
    NSInteger index = btn.tag%2;
    LoverPicModel * model = _loverArray[section];
    DownloadViewController * downloadVC = [[DownloadViewController alloc]initWithNibName:@"DownloadViewController" bundle:nil];
//    downloadVC.picType = 3;
    downloadVC.isLongPic = YES;
    downloadVC.dataSource = [NSMutableArray arrayWithObjects:model, nil];;
    downloadVC.picIndex = index;
    downloadVC.isLoverVc = YES;
    downloadVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:downloadVC animated:YES];
}

-(void)typeSelectChange:(UIButton *)btn
{
    [UIView animateWithDuration:0.2f animations:^{
        [self.scroll setContentOffset:CGPointMake(rScreenWidth * btn.tag, 0)];
    }];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scroll) {
        NSInteger index = (scrollView.contentOffset.x + rScreenWidth/2 )/rScreenWidth;
        [_scroll setContentSize:CGSizeMake(rScreenWidth * 5, 0)];

        [switchView scrollButtons:index];
    }
}

-(void)setViewCornerRadius:(UIView *)view isHead:(BOOL)isHead
{
    UIBezierPath *maskPath;
    if (isHead)
        maskPath= [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    else
        maskPath= [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
